package com.xj.zhihudemo.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.facebook.drawee.view.SimpleDraweeView;
import com.xj.zhihudemo.R;
import com.xj.zhihudemo.base.FullLayoutActivity;
import com.xj.zhihudemo.mode.SplashInfo;
import com.xj.zhihudemo.retrofit.ApiClient;
import com.xj.zhihudemo.retrofit.ApiStore;
import com.xj.zhihudemo.utils.ActivityUtils;
import com.xj.zhihudemo.utils.HandlerUtils;

import butterknife.BindView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class SplashActivity extends FullLayoutActivity implements Runnable {
    private final String TAG = this.getClass().getSimpleName();
    @BindView(R.id.iv_splash)
    SimpleDraweeView mIvSplash;
    private Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        HandlerUtils.postDelayed(this, 3000);

    }

    @Override
    protected int getLayoutID() {
        return R.layout.activity_splash;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {

    }
    protected void initData() {


        ApiStore apiStore = ApiClient.retrofit().create(ApiStore.class);
        Call<SplashInfo> splashCall = apiStore.getSplash("1080*720");
        splashCall.enqueue(new Callback<SplashInfo>() {
            @Override
            public void onResponse(Call<SplashInfo> call, Response<SplashInfo> response) {

                Log.d(TAG, "访问成功");
                Log.d(TAG, response.toString());
                SplashInfo splashInfo = response.body();
                String imageUrl = splashInfo.getImg();
                String splashText = splashInfo.getText();
                Log.d(TAG, imageUrl);
                if (!"".equals(imageUrl)) {
                    mIvSplash.setImageURI(imageUrl);
                }

            }

            @Override
            public void onFailure(retrofit2.Call<SplashInfo> call, Throwable t) {
                Log.d(TAG, "访问失败");

            }
        });
    }


    @Override
    public void run() {
        if (ActivityUtils.isAlive(this)) {
            startActivity(new Intent(this, MainActivity.class));
            finish();
        }
    }
}
